package com.zhangxujie.litewords.ui.achievement;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;

import com.zhangxujie.litewords.R;
import com.zhangxujie.litewords.db.AppDatabase;
import com.zhangxujie.litewords.db.DBUtil;
import com.zhangxujie.litewords.entity.Achievement;
import com.zhangxujie.litewords.ui.achievement.model.AchiInfo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class AchiFragment extends Fragment {

    private AchiViewModel achiViewModel;


    public View onCreateView(@NonNull final LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        achiViewModel =
                ViewModelProviders.of(this).get(AchiViewModel.class);
        final View root = inflater.inflate(R.layout.fragment_achi, container, false);


        final List<AchiInfo> achiInfos = new ArrayList();

        final ListView achiList = root.findViewById(R.id.achi_list);

        final ListAdapter adapter = new BaseAdapter() {
            @Override
            public int getCount() {
                return achiInfos.size();
            }

            @Override
            public Object getItem(int position) {
                return achiInfos.get(position);
            }

            @Override
            public long getItemId(int position) {
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = null;
                if (convertView == null) {
                    view = inflater.inflate(R.layout.achi_list_item, null);
                } else {
                    view = convertView;
                }
                AchiInfo achi = achiInfos.get(position);

                TextView achiName = view.findViewById(R.id.achi_name);
                TextView achiTime = view.findViewById(R.id.achi_time);
                ImageView achiStatus = view.findViewById(R.id.achi_status);

                if (achiName != null && achiTime != null) {
                    achiName.setText(achi.getAchiName());
                    achiName.setTextSize(14);
                    achiTime.setText(achi.getTime());
                    achiTime.setTextSize(8);

                    if (achi.getTime().equals("未解锁")) {
                        achiStatus.setImageDrawable(getResources().getDrawable(R.drawable.ic_achi_no));
                    } else {
                        achiStatus.setImageDrawable(getResources().getDrawable(R.drawable.ic_achi_yes));
                    }
                }
                return view;
            }
        };

        @SuppressLint("HandlerLeak") final Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                List<Achievement> achievementList = (List<Achievement>) msg.obj;

                for (Achievement a : achievementList) {
                    Log.d("Achievement", "run: " + a.toString());
                    String timestamp;
                    AchiInfo info;
                    if (a.status == 0) {
                        info = new AchiInfo(a.achievementName, "未解锁");
                    } else {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        info = new AchiInfo(a.achievementName, sdf.format(new Date()));
                    }
                    achiInfos.add(info);
                }
                achiList.setAdapter(adapter);

                super.handleMessage(msg);
            }
        };
        new Thread() {

            @Override
            public void run() {
                AppDatabase db = DBUtil.getDb(getContext());
                List<Achievement> achievementList = db.achievementDAO().getAll();
                Message message = new Message();
                message.obj = achievementList;
                handler.sendMessage(message);
            }
        }.start();

        return root;
    }

}
